package com.centersoft.flowable.db.task.gateway.impl;

import com.centersoft.flowable.core.dto.TaskDefExtensionRequest;
import com.centersoft.flowable.core.model.FlowAssigneeModel;
import com.centersoft.flowable.core.model.TaskDefExtension;
import com.centersoft.flowable.core.web.gateway.TaskDefExtensionGateway;
import com.centersoft.flowable.db.task.convert.FlowAssigneeConvert;
import com.centersoft.flowable.db.task.convert.FlowButtonsConvert;
import com.centersoft.flowable.db.task.convert.TaskDefExtensionConvert;
import com.centersoft.flowable.db.task.dao.FlowAssigneeDao;
import com.centersoft.flowable.db.task.dao.FlowButtonsDao;
import com.centersoft.flowable.db.task.dao.TaskDefExtensionDao;
import com.centersoft.flowable.db.task.entity.FlowAssigneeEntity;
import com.centersoft.flowable.db.task.entity.FlowButtonsEntity;
import com.centersoft.flowable.db.task.entity.TaskDefExtensionEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class TaskDefExtensionGatewayImpl implements TaskDefExtensionGateway {

    private final TaskDefExtensionDao taskDefExtensionDao;
    private final FlowAssigneeDao flowAssigneeDao;
    private final FlowButtonsDao flowButtonsDao;

    public TaskDefExtensionGatewayImpl(TaskDefExtensionDao taskDefExtensionDao, FlowAssigneeDao flowAssigneeDao, FlowButtonsDao flowButtonsDao) {
        this.taskDefExtensionDao = taskDefExtensionDao;
        this.flowAssigneeDao = flowAssigneeDao;
        this.flowButtonsDao = flowButtonsDao;
    }

    @Override
    public TaskDefExtension get(String id) {
        TaskDefExtensionEntity taskDefExtensionEntity = taskDefExtensionDao.findOneById(id);

        TaskDefExtension taskDefExtension = null;
        if (taskDefExtensionEntity != null) {
            taskDefExtension = TaskDefExtensionConvert.toModel(taskDefExtensionEntity);
            List<FlowAssigneeEntity> assignees = flowAssigneeDao.list(id);
            List<FlowButtonsEntity> buttons = flowButtonsDao.list(id);

            if (!CollectionUtils.isEmpty(assignees)) {
                taskDefExtension.setFlowAssigneeList(assignees.stream().map(FlowAssigneeConvert::toModel).collect(Collectors.toList()));

            }
            if (!CollectionUtils.isEmpty(buttons)) {
                taskDefExtension.setFlowButtonList(buttons.stream().map(FlowButtonsConvert::toModel).collect(Collectors.toList()));
            }
        }
        return taskDefExtension;
    }

    @Override
    public List<TaskDefExtension> findList(TaskDefExtensionRequest taskDefExtension) {
        List<TaskDefExtensionEntity> list = taskDefExtensionDao.list(taskDefExtension);
        return list.stream().map(TaskDefExtensionConvert::toModel).collect(Collectors.toList());

    }

    @Override
    public List<FlowAssigneeModel> getFlowAssigneeList(String taskDefId) {
        return flowAssigneeDao.list(taskDefId).stream().map(FlowAssigneeConvert::toModel).collect(Collectors.toList());
    }
}
